package edu.cueb.wmis.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import edu.cueb.wmis.pojo.House;
import edu.cueb.wmis.pojo.Incomebillitem;
import edu.cueb.wmis.pojo.RespBean;
import edu.cueb.wmis.pojo.Shelf;
import edu.cueb.wmis.service.IGoodService;
import edu.cueb.wmis.service.IHouseService;
import edu.cueb.wmis.service.IIncomebillitemService;
import edu.cueb.wmis.service.IShelfService;
import edu.cueb.wmis.utils.ShelfUtils;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/incomebillitem")
public class IncomebillitemController {

    @Autowired
    private IIncomebillitemService iIncomebillitemService;
    @Autowired
    private IGoodService goodService;

    @ApiOperation(value = "入库添加")
    @PostMapping("/")
    public RespBean addInBillItem(@RequestBody Incomebillitem incomebillitem){

        if(iIncomebillitemService.addInBillItem(incomebillitem) && goodService.addGood(incomebillitem)){

            return RespBean.success("入库成功");
        }else{
            return RespBean.error("入库物品失败!");
        }
    }

    @ApiOperation("批量加入到入库详单")
    @PostMapping("/import")
    public RespBean importInBillItem(MultipartFile file) {
        ImportParams params = new ImportParams();

        try{
            List<Incomebillitem> list = ExcelImportUtil.importExcel(file.getInputStream(), Incomebillitem.class, params);
            for(Incomebillitem item : list){
                ShelfUtils.setShelves(item);
                iIncomebillitemService.addInBillItem(item) ;
                goodService.addGood(item);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return RespBean.success("1");
    }
}
